package com.gxa.controller;

import com.alibaba.fastjson.JSON;
import com.gxa.pojo.Store;
import com.gxa.service.DepartmentService;
import com.gxa.pojo.Department;
import com.gxa.service.LogService;
import com.gxa.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/department")
@CrossOrigin
public class DepartmentController {

    @Autowired
    DepartmentService departmentService;
    @Autowired
    LogService logService;


    JsonResult jsonResult=null;

// number 页码    size：每页显示记录数

    @ResponseBody
    @RequestMapping(value = "/departmentPage")
    public Map findAllDepartmentByPage(@RequestBody Map map){
        String  number= map.get("number").toString();
        String size= map.get("size").toString();
        System.out.println("number:"+number);
        System.out.println("size:"+size);
        Integer offset =(Integer.parseInt(number)-1)*Integer.parseInt(size);
        Integer limit=Integer.parseInt(size);
        List<Department> departments=departmentService.findAllDepartmenByPage(offset,limit);
        Integer count=departmentService.findAllDepartment();   // select count(*) from
        jsonResult = new JsonResult();
        jsonResult.setData(departments);
        jsonResult.put("count",count);
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/findDepartmentList")
    public List<Department> findAll(){
        List<Department> departmentList = departmentService.findAll();
        return departmentList;
    }

    @ResponseBody
    @RequestMapping("/departmentEdit")
    public Map updateDepartmentById(@RequestBody Map map){
        jsonResult=new JsonResult();
        System.out.println(map);
        Date date=new Date();
        Map log = (Map) (map.get("log"));
        log.put("operationTime",date);
        int i = logService.insertLog(log);
        int n = departmentService.updateDepartmentById(map);
        if(n>0){
           jsonResult.setCode("200");
           jsonResult.setMsg("修改成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开了小差");
        }
        return jsonResult.getValues();
    }



    @ResponseBody
    @RequestMapping("/departmentDel")
    public Map deleteDepartmentById(@RequestBody Map map){
        jsonResult=new JsonResult();
        System.out.println(map);
        Date date=new Date();
        Map log = (Map) (map.get("log"));
        log.put("operationTime",date);
        int i = logService.insertLog(log);

        String id=map.get("departmentID").toString();
        int n=departmentService.deleteDepartmentById(Integer.valueOf(id));
        if(n>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开了小差");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/departmentAdd")
    public Map insertDepartment(@RequestBody Map map) throws Exception {
        jsonResult=new JsonResult();
        System.out.println(map);
        String name = map.get("name").toString();
        String departmentByName = departmentService.findDepartmentByName(name);
        if (departmentByName!=null){
            jsonResult.setCode("401");
            jsonResult.setMsg("该部门已存在！");
            return jsonResult.getValues();
        }
        Date date=new Date();
        Map log = (Map) (map.get("log"));
        String userID = log.get("userID").toString();
        System.out.println(userID);
        String descript = log.get("descript").toString();
        System.out.println(descript);
        log.put("operationTime",date);
        int i = logService.insertLog(log);
        map.put("addTime",date);
//        String s=map.get("addTime").toString();
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date parse = dateFormat.parse(s);
        map.put("addTime",date);
        System.out.println("map:"+toString());
        int n=departmentService.insertDepartment(map);
        if(n>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("添加成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开了小差");
        }
        return jsonResult.getValues();
    }

    @ResponseBody
    @RequestMapping("/departmentState")
    public Map departmentState(@RequestBody Map map){
        jsonResult=new JsonResult();
        System.out.println(map);
        Date date=new Date();
        Map log = (Map) (map.get("log"));
        log.put("operationTime",date);
        int i = logService.insertLog(log);
        int n=departmentService.departmentState(map);
        if(n>0){
            jsonResult.setCode("200");
            jsonResult.setMsg("状态修改成功");
        }else {
            jsonResult.setCode("406");
            jsonResult.setMsg("系统开了小差");
        }
        return jsonResult.getValues();
    }

}